#include "client.h"
#include <QHostAddress>
#include <QMessageBox>
#include <QDebug>
Client::Client(QString serverIp, quint16 serverPort, QObject *parent) : QObject(parent)
{
   serverAddress = new QHostAddress();
   if(!serverAddress->setAddress(serverIp)){
        QMessageBox::warning(nullptr, "错误","ip地址格式不正确");
   }
   this->serverPort = serverPort;
}

//连接服务端
void Client::connect(){
    if(socket == nullptr){
        socket = new QTcpSocket(this);
    }
    socket->connectToHost(*serverAddress, serverPort);
    //绑定事件
   QObject::connect(socket, SIGNAL(connected()), SLOT(connectSuccess()));
   QObject::connect(socket, SIGNAL(readyRead()), SLOT(readServerData()));
}
 //断开连接
void Client::disConnect(){
    socket->close();
}

//连接成功
void Client::connectSuccess(){
    //qDebug() << "连接服务端成功" <<endl;
   // socket->write(QByteArray("你好，服务端"));
}
//读取数据
void Client::readServerData(){
   QByteArray array = socket->readAll();
   QString s(array);
   emit(signalReceiveMsg(s));//发出去
}














